<?php
/**
 * @ Fmash Top System
 * @ version: 3.0
 * @ author: i.coffee.smile@mail.com
 * @ date:2011-1-27
 * @ file:function_fav.php $10
 */
 
//以下是获取UID之前已经喜欢的妹子ID
function heart_q($id)
{
	$uid = $_COOKIE['UID'];
	$sql = "SELECT `fav` FROM `top_member` WHERE `uid` = '$uid'";
	$result = mysql_query($sql);
	$fav = mysql_fetch_row($result);
	$exp_fav = explode("|",$fav[0]);
	for($i=0,$favLen=count($exp_fav);$i<$favLen;$i++)
	{
		$expfav = explode(",",$exp_fav[$i]);
	
		$favArr[] = array('uid'=>$expfav[0]);
		$favidArr[$i] = $favArr[$i]['uid']; 
	}
	if(in_array($id,$favidArr))
	{
		$message = "heart hearted";
	}else{
		$message = "heart";
	}
	return $message;
}

/**
 * @ date:2010-12-16 
 * @ 删除UID喜欢的妹子的ID,这里传值进去的ID是妹子的ID，而非用户
 */
function heart_del($id)
{
	heart_right($id);	
	$numid = $id;
	$uid = $_COOKIE['UID'];
	$sql = "SELECT `fav` FROM `top_member` WHERE `uid` = '$uid'";
	$result = mysql_query($sql);
	$fav = mysql_fetch_row($result);
	$exp_fav = explode("|",$fav[0]);
	for($i=0,$favLen=count($exp_fav);$i<$favLen;$i++)
	{
		$expfav = explode(",",$exp_fav[$i]);
	
		$favArr[$i] = array('uid'=>$expfav[0],'name'=>$expfav[1],'show'=>$expfav[2]);
		
		if($favArr[$i]['uid'] == $numid){
			unset($exp_fav[$i]);
			break;
		}
	}
	$favs = array_to_str($exp_fav);
	$up_sql = "UPDATE `top_member` SET `fav` = '$favs' WHERE `uid` = '$uid'";
	mysql_query($up_sql);
	del_faveduid($id);
}

function heart_in($id)
{
	heart_right($id);
	//get girls information
	$sql_get = "SELECT `name`,`show` FROM `top_girls` WHERE `sid` = '$id'";
	$get = mysql_query($sql_get);
	$getinfo = mysql_fetch_row($get);
	$girlsname = $getinfo[0];
	$girlsshow = $getinfo[1];

	//get list of user lovely girls 
	$uid = $_COOKIE['UID'];
	$uname = $_COOKIE['Uname'];
	$time = time();
	//查找UID是否在数据库里面，必须要在里面
	if(in_mysql_member($uid))
	{
		$sql = "SELECT `fav` FROM `top_member` WHERE `uid` = '$uid'";
		$result = mysql_query($sql);
		$fav = mysql_fetch_row($result);
		$ifav = $fav[0];
		$exp_fav = explode("|",$fav[0]);
		for($i=0,$favLen=count($exp_fav);$i<$favLen;$i++)
		{
			$expfav = explode(",",$exp_fav[$i]);
			$favArr[] = array('uid'=>$expfav[0],'name'=>$expfav[1],'show'=>$expfav[2]);
			$favidArr[$i] = $favArr[$i]['uid']; 
		}//如果进来的美女ID在这个UID里面，就删除
		if(in_array($id,$favidArr))//if id in favidArr
		{
			heart_del($id);//delete it!
		}else{
			//if this is first of all
			if($fav[0]==''){
				$up_sql = "UPDATE `top_member` SET `fav` = '$id,$girlsname,$girlsshow',`lastdate` = '$time' WHERE `uid` = '$uid'";
			}else{
				$ifav .= "|".$id.",".$girlsname.",".$girlsshow;	
				$up_sql = "UPDATE `top_member` SET `fav` = '$ifav',`lastdate` = '$time' WHERE `uid` = '$uid'";
			}
			mysql_query($up_sql);
			in_faveduid($id);
		}
	}else{
		$sql = "INSERT INTO `top_member` (`uid` ,`name` ,`fav` ,`lastdate` ,`key` )VALUES ('$uid', '$uname', '$id,$girlsname,$girlsshow' , '$time', NULL )";
		mysql_query($sql);
		in_faveduid($id);
		
	}
	//up_hearts($id);
}

function heart_right($id)
{
	$id = incheck($id);
	$count = mysql_query("SELECT count(*) FROM `top_girls` WHERE `sid`='$id'" );
	$c = mysql_fetch_row($count);
	if($c[0] == 0)
	{
		exit("error:your enter the num is too large!!");
	}
}

//
function top_fav_num($id)
{
	$sql = "SELECT `faveduid` FROM `top_girls_fav` WHERE `sid`='$id'";
	$result = mysql_query($sql);
	$fav = mysql_fetch_row($result);
	$exp_fav = explode("|",$fav[0]);
	return count($exp_fav);
}

//我们这里是插入喜欢这个MM的用户UID到MM的数据库里
function in_faveduid($id)
{
	$uid = $_COOKIE['UID'];
	$uname = $_COOKIE['Uname'];
	//查找这个MM所有喜欢她的用户UID
	if(in_mysql_girlsfav($id))
	{
		$sql_get_faveduid = "SELECT `faveduid` FROM `top_girls_fav` WHERE `sid` = '$id'";
		$get_faveduid = mysql_query($sql_get_faveduid);
		$faveduid = mysql_fetch_row($get_faveduid);
		$exp_faveduid = explode("|",$faveduid[0]);
		//我们可以假设里面有一些数组
		for($i=0,$faveduidLen=count($exp_faveduid);$i<$faveduidLen;$i++)
		{
			$expfaveduid = explode(",",$exp_faveduid[$i]);
			$faveduidArr[] = array('uid'=>$expfaveduid[0],'name'=>$expfaveduid[1]);
			$faveduidArray[$i] = $faveduidArr[$i]['uid']; 
		}
		if(in_array($uid,$faveduidArray))
		{
			echo "存在这个用户UID";//delete it!
		}else{
			//if this is first of all
			if($faveduid[0]==''){
				$sql = "UPDATE `top_girls_fav` SET `faveduid` = '$uid,$uname' WHERE `sid` = '$id'";
			}else{
				$faveduid[0] .= "|".$uid.",".$uname;
				$sql = "UPDATE `top_girls_fav` SET `faveduid` = '$faveduid[0]' WHERE `sid` = '$id'";
			}
			//exit($sql);
			mysql_query($sql);
		}
	}else{
		$sql = "INSERT INTO `top_girls_fav` (`sid` ,`faveduid`)VALUES ('$id', '$uid,$uname')";
		mysql_query($sql);
	}
}

//这是操作哪位MM的数据库
function del_faveduid($id)
{
	$uid = $_COOKIE['UID'];
	$sql = "SELECT `faveduid` FROM `top_girls_fav` WHERE `sid` = '$id'";
	$result = mysql_query($sql);
	$faveduid = mysql_fetch_row($result);
	$exp_faveduid = explode("|",$faveduid[0]);
	for($i=0,$faveduidLen=count($exp_faveduid);$i<$faveduidLen;$i++)
	{
		$expfaveduid = explode(",",$exp_faveduid[$i]);
	
		$faveduidArr[$i] = array('uid'=>$expfaveduid[0],'name'=>$expfaveduid[1]);
		
		if($faveduidArr[$i]['uid'] == $uid){
			unset($exp_faveduid[$i]);
			break;
		}
	}
	$faveduids = array_to_str($exp_faveduid);
	$up_sql = "UPDATE `top_girls_fav` SET `faveduid` = '$faveduids' WHERE `sid` = '$id'";
	mysql_query($up_sql);
}

function in_mysql_member($uid)
{
	$sql = "SELECT count(*) FROM `top_member` WHERE `uid` ='$uid'";
	$result = mysql_query($sql);
	$row = mysql_fetch_row($result);
	if($row[0]>0){
		$unempty = 1;
	}else{
		$unempty = 0;
	}
	return $unempty;//在数据库里显示应该为1
}

function in_mysql_girlsfav($sid)
{
	$sql = "SELECT count(*) FROM `top_girls_fav` WHERE `sid` ='$sid'";
	$result = mysql_query($sql);
	$row = mysql_fetch_row($result);
	if($row[0]>0){
		$unempty = 1;
	}else{
		$unempty = 0;
	}
	return $unempty;//在数据库里显示应该为1
}

function how_many_fav($sid)
{
	$sql = "SELECT `faveduid` FROM `top_girls_fav` WHERE `sid` = '$sid'";
	$result = mysql_query($sql);
	$row = mysql_fetch_row($result);
	if($row[0] == '' || $row[0] == ',' || $row[0] == '|')
	{
		$num = 0;
	}else{
		$exp = explode("|",$row[0]);
		$num = count($exp);
	}
	return $num;
}

//增加一个喜欢者
function up_hearts($sid)
{
	$sql = "UPDATE `top_girls` SET `hearts` = `hearts` + '1' WHERE `sid` = '$sid'";
	mysql_query($sql);
}

function down_hearts($sid)
{
	$sql = "UPDATE `top_girls` SET `hearts` = `hearts` + '-1' WHERE `sid` = '$sid'";
	mysql_query($sql);
}